package com.example.demo.mapper;

import com.example.demo.entity.Message;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;

import java.util.List;

@Mapper
public interface MessageMapper {
    @Insert("INSERT INTO messages (sender, receiver, content, timestamp) VALUES (#{sender}, #{receiver}, #{content}, CURRENT_TIMESTAMP)")
    void sendMessage(@Param("sender") int sender, @Param("receiver") int receiver, @Param("content") String content);

    @Select("SELECT * FROM messages WHERE (sender = #{user_id} AND receiver = #{targetUser_id}) OR (sender = #{targetUser_id} AND receiver = #{user_id}) ORDER BY timestamp")
    List<Message> getMessages(@Param("user_id") int user_id, @Param("targetUser_id") int targetUser_id);

    @Select("SELECT * FROM messages WHERE (sender = #{user_id} AND receiver = #{targetUser_id}) " +
            "OR (sender = #{targetUser_id} AND receiver = #{user_id}) ORDER BY timestamp ASC")
    List<Message> getConversation(@Param("user_id") int user_id, @Param("targetUser_id") int targetUser_id);

}